package com.ruoyi.eqmonitor.temporary.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.eqmonitor.domain.OptDeviceAlarmProcess;
import com.ruoyi.eqmonitor.temporary.domain.Smdtv28;
import com.ruoyi.eqmonitor.temporary.dto.ExcelDto;
import com.ruoyi.eqmonitor.temporary.service.Smdtv28Service;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.Field;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 弃用
 */
@Api(tags = "临时接口")
@Slf4j
@RestController
@RequestMapping("/smdtv28")
public class Smdtv28Controller {

    @Resource
    private Smdtv28Service smdtv28Service;

    @Log
    @GetMapping("/issue")
    public AjaxResult issue(String szdl) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("台州普查问题描述清单.xlsx");
        ExcelUtil<ExcelDto> excelDtoExcelUtil = new ExcelUtil<>(ExcelDto.class);
        Map<String, Object> maps = new HashMap<>();
        try(
            InputStream is = classPathResource.getInputStream();
        ){
            List<ExcelDto> excelDtos = excelDtoExcelUtil.importExcel(is);
            //根据街道进行分组
            Map<String, List<ExcelDto>> collect = excelDtos.stream().collect(Collectors.groupingBy(ExcelDto::getArea));
            if (szdl == null || szdl.equals("")) {
                for (Map.Entry<String, List<ExcelDto>> entry : collect.entrySet()) {
                    List<ExcelDto> value = entry.getValue();
                    Map<String, Object> map = smdtv28Service.getInfoByWtdh(value);
                    maps.put(entry.getKey(), map);
                }
            } else {
                for (Map.Entry<String, List<ExcelDto>> entry : collect.entrySet()) {
                    if (entry.getKey().equals(szdl)) {
                        List<ExcelDto> value = entry.getValue();
                        maps = smdtv28Service.getInfoByWtdh(value);
                        break;
                    }
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return AjaxResult.success(maps);
    }

}
